package com.sansan.qiangji.controller.open;

import com.sansan.qiangji.resultVO.ResultVO;
import com.sansan.qiangji.task.AsyncTask;
import com.sansan.qiangji.utils.ResultVOUtil;
import com.sansan.qiangji.utils.plcUtils.HCPlcUtil;
import com.sansan.qiangji.utils.tcpUtils.YRTcpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@CrossOrigin()
@RestController
@RequestMapping("/open")
@Slf4j
public class OpenController {
    private AsyncTask asyncTask;

    @Resource
    public void setAsyncTask(AsyncTask asyncTask){
        this.asyncTask = asyncTask;
    }

    @GetMapping("/test")
    public ResultVO<Object> test() {
        YRTcpUtil yrTcpUtil = new YRTcpUtil("192.168.0.7", 23);
        boolean result = yrTcpUtil.isConnect();
        String ver = yrTcpUtil.checkScanVer();
        String code = "";
        if (ver!=null) {
            yrTcpUtil.openScan();
            code = yrTcpUtil.receiveScanData(3000);
        }
        yrTcpUtil.disConnect();
        /*TDPlcUtil tdPlcUtil = new TDPlcUtil("192.168.0.3", 502);
        for (int i = 0; i<100; i++){
            Integer[] result = tdPlcUtil.readStorageWord(34, 1);
        }
        tdPlcUtil.disConnect();*/
        return ResultVOUtil.success(code);
    }

    @GetMapping("/readCoil")
    public ResultVO<Object> readCoil(@RequestParam(name = "address") Integer address) {
        HCPlcUtil hcPlcUtil = new HCPlcUtil("192.168.1.1", 502);
        Boolean[] result = hcPlcUtil.readCoil(address, 1);
        if (result.length == 1 && result[0]){
            return ResultVOUtil.success("true");
        }
        if (result.length == 1){
            return ResultVOUtil.success("false");
        }
        hcPlcUtil.disConnect();
        return ResultVOUtil.success("读取失败");
    }

    @GetMapping("/readStorage")
    public ResultVO<Object> readStorage(@RequestParam(name = "address") Integer address) {
        HCPlcUtil hcPlcUtil = new HCPlcUtil("192.168.1.1", 502);
        Integer[] result = hcPlcUtil.readStorage(address, 1);
        if (result.length == 1){
            return ResultVOUtil.success(result[0].toString());
        }
        hcPlcUtil.disConnect();
        return ResultVOUtil.success("读取失败");
    }

    @GetMapping("/writeCoil")
    public ResultVO<Object> writeCoil(@RequestParam(name = "address") Integer address, @RequestParam(name = "value") Integer value) {
        HCPlcUtil hcPlcUtil = new HCPlcUtil("192.168.1.1", 502);
        if (value == 1) {
            hcPlcUtil.writeCoil(address, new Boolean[]{true});
        }else if (value == 0) {
            hcPlcUtil.writeCoil(address, new Boolean[]{false});
        }
        hcPlcUtil.disConnect();
        return ResultVOUtil.success(null);
    }

    @GetMapping("/writeStorage")
    public ResultVO<Object> writeStorage(@RequestParam(name = "address") Integer address, @RequestParam(name = "value") Integer value) {
        HCPlcUtil hcPlcUtil = new HCPlcUtil("192.168.1.1", 502);
        hcPlcUtil.writeStorage(address, new int[]{value});
        hcPlcUtil.disConnect();
        return ResultVOUtil.success(null);
    }
}
